import Cookies from 'js-cookie'
import HttpService from './baseHttp'
import { handleHostname } from '../utils/tools'

export const SOCIAL_HOST_STUB_THEBIZARK='.thebizark.com';
export const SOCIAL_HOST_STUB_BIZARKDEV='.bizarkdev.com';
export const SOCIAL_HOST_STUB_QLINYUN='.qlinyun.com';
export const SOCIAL_HOST_STUB_QLINYUNDEV='.qlinyun.com';

export const SOCIAL_VENDOR_DINGTALK='dingtalk';
export const SOCIAL_VENDOR_WECHAT='wechat';
export const SOCIAL_VENDOR_WECHAT_OPEN='wechat_open';
export const SOCIAL_VENDOR_WECHAT_MP='wechat_mp';
export const SOCIAL_VENDOR_WECHAT_ENTERPRISE='wechat_enterprise';
export const SOCIAL_VENDOR_THEBIZARK='thebizark';
export const SOCIAL_VENDOR_BIZARKDEV='bizarkdev';
export const SOCIAL_VENDOR_QLINYUN='qlinyun';
export const SOCIAL_VENDOR_QLINYUNDEV='qlinyundev';

// 用户信息
export const HttpUserInfo = (data = {}) => HttpService.get('/api/v1/My/getUserProfile', { params: data, baseURL: process.env.VUE_APP_APIURL_UC })

// 获取系统
export const HttpSystemList = (data = {}, config) => HttpService.post('/api/v1/Context/findByPage', data, { ...config, baseURL: process.env.VUE_APP_APIURL_UC })

// 获取当前系统下所有权限列表
export const HttpMenuList = (data = {}) => HttpService.get('/api/v1/Context/contextPermissions', { params: data, baseURL: process.env.VUE_APP_APIURL_UC })

// 退出
export const HttpLogout = (data = {}, config) => HttpService.post('/auth/jwt/logout', data, { ...config, baseURL: process.env.VUE_APP_APIURL_UC });

// 登录
export const HttpLogin = (data = {}, config) => HttpService.post('/auth/jwt/authorize', data, { ...config, baseURL: process.env.VUE_APP_APIURL_UC });

//创建人列表
export const HttpUserList = (data = {}) => HttpService.get("/api/v1/Context/contextPermission", { params: data, baseURL: process.env.VUE_APP_APIURL_UC });

//获得全局关联数据读取
export const HttpModuleList = (data = {}) => HttpService.get("/api/v1/Context/globalDataList", { params: data, baseURL: process.env.VUE_APP_APIURL_UC });

//获得多语言
export const HttpLangList = (data = {}) => HttpService.get("/status/locale/findByFilters", { params: data, baseURL: process.env.VUE_APP_APIURL_UC });


// 用户收藏菜单保存
export const HttpSaveNavMenu = (data = {}, config) => HttpService.post('/api/v1/UcUserFavMenu/saveAll', data, { ...config, baseURL: process.env.VUE_APP_APIURL_UC });
// HttpService.post('/api/v1/UcUserFavMenu/saveAll', data, { headers: { "Content-Type": "application/json" }, params: { contextId: global.SYSID } , baseURL: process.env.VUE_APP_APIURL_UC}); 

 
//用户收藏菜单ids读取
export const HttpNavMenu = (data = {}) => HttpService.get("/api/v1/UcUserFavMenu/findAll", { params: data, baseURL: process.env.VUE_APP_APIURL_UC });


// 附件删除
export const HttpDelFile = (data = {}) => HttpService.get('/api/v1/BizAttach/del', { params: data, baseURL: process.env.VUE_APP_APIURL_UC });

// 查找erpsku
export const HttpGetErpSku = (data = {}) => HttpService.get('/api/v1/Product/findByPage', { params: data, baseURL: process.env.VUE_APP_APIURL_BOSS })
export const HttpGetManageErpSku = (data = {}) => HttpService.get('/api/v2/productManage/findByPage', { params: data, baseURL: process.env.VUE_APP_APIURL_BOSS })

// 获取订单照片
export const HttpGetOrderPicture = (data = {}) => HttpService.get('/api/v2/orderPicture/findByFilters', { params: data, baseURL: process.env.VUE_APP_APIURL_FBB })


// 全局搜索
// 公告搜索
export const HttpSearchAnnounce = (data = {}) => HttpService.get('/api/v1/BizAnnouncement/gsmod', { params: data, baseURL: process.env.VUE_APP_APIURL_UC })
export const HttpSearchOrder = (data = {}) => HttpService.get('/api/v2/B2cOrder/gsmod', { params: data, baseURL: process.env.VUE_APP_APIURL_BOSS })
export const HttpSearchProduct = (data = {}) => HttpService.get('/api/v1/Product/gsmod', { params: data, baseURL: process.env.VUE_APP_APIURL_BOSS })
export const HttpSearchPO = (data = {}) => HttpService.get('/api/v2/po/gsmod', { params: data, baseURL: process.env.VUE_APP_APIURL_BOSS })

export const HttpUpload = (url, data = {}) => HttpService.post(url, data, {
    headers: {
        "Content-Type": "multipart/form-data",
    },
    transformRequest(data) {
        return data
    }, paramsSerializer(params) {
        return params;
    }
})

export const LogoutBase = function (clearCache) {
    console.log(clearCache)
    // 获取用户信息
    const userInfo = localStorage.getItem('USERINFO');
    // const organization = Cookies.get("organization");
    let organization = Cookies.get("organization");
    // if (organization) {
    //     let jsonOrg = JSON.parse(organization);
    //     // 修改organization结构后需要清空cookie，否则除了切换组织，旧的结构会一直保存在cookie中
    //     if (!jsonOrg.roleLabel) organization = null;
    // }
    const lang = Cookies.get("lang");
    // const showtab = Cookies.get("showtab");
    // const tabnavlist = Cookies.get("TABNAVLIST");
    localStorage.removeItem("MODULELIST");
    localStorage.removeItem("SYSSCOPELIST");
    localStorage.removeItem("NOTICEEVENTTYPES");
    sessionStorage.removeItem("MENULIST");

    sessionStorage.clear(true);
    localStorage.clear(clearCache);
    Object.keys(Cookies.get()).forEach(key => {
        Cookies.remove(key, { domain: global.DOMAIN });
    })
    let userId = ''
    if (userInfo) {
        userId = JSON.parse(userInfo).id;
    }
    // 添加上一个登录的用户信息
    Cookies.set('userId', userId, { expires: 14, domain: global.DOMAIN });
    if (organization) Cookies.set('organization', organization, { domain: global.DOMAIN, expires: 7 });
    if (lang) Cookies.set('lang', lang, { domain: global.DOMAIN, expires: 7 });
    // if (showtab) Cookies.set('showtab', showtab, { domain: global.DOMAIN, expires: 7 });
    // if (tabnavlist) Cookies.set('TABNAVLIST', tabnavlist, { domain: global.DOMAIN, expires: 7 });
    location.href = handleHostname(`http://localhost:8080/login`);
}

export const HttpSocialAuthRender = (source= SOCIAL_VENDOR_DINGTALK,host='',redirectUrl = '', data = {}) =>  HttpService.get(`/auth/connect/render/${source}?host=${host}&redirectUrl=${redirectUrl}`, { params: data, baseURL: process.env.VUE_APP_APIURL_UC });

export const HttpSocialAuthCallback = (source= SOCIAL_VENDOR_DINGTALK,host='',data = {}, config) =>  HttpService.post(`/auth/connect/callback/${source}?host=${host}`, data,{ ...config, baseURL: process.env.VUE_APP_APIURL_UC });

export const HttpSocialAuthRevoke = (source= SOCIAL_VENDOR_DINGTALK,uuid='',host='', data = {}) =>  HttpService.get(`/auth/connect/revoke/${source}/${uuid}?host=${host}`, { params: data, baseURL: process.env.VUE_APP_APIURL_UC });

export const HttpSocialAuthRefresh = (source= SOCIAL_VENDOR_DINGTALK,uuid='',host='', data = {}) =>  HttpService.get(`/auth/connect/refresh/${source}/${uuid}?host=${host}`, { params: data, baseURL: process.env.VUE_APP_APIURL_UC });

export const HttpSocialAuthUpdateAssignData = (source= SOCIAL_VENDOR_DINGTALK,uuid='',host='',data = {}, config) =>  HttpService.post(`/auth/connect/updateAssignData/${source}/${uuid}?host=${host}`, data,{ ...config, baseURL: process.env.VUE_APP_APIURL_UC });

export const HttpSocialAuthUnbindSource = (source= SOCIAL_VENDOR_DINGTALK,host='',data = {}, config) =>  HttpService.post(`/auth/connect/unbind/${source}?host=${host}`, data,{ ...config, baseURL: process.env.VUE_APP_APIURL_UC });

export const HttpSocialAuthBound = (host='',data = {}, config) =>  HttpService.post(`/auth/connect/bound?host=${host}`, data,{ ...config, baseURL: process.env.VUE_APP_APIURL_UC });
